<?php /* Smarty version 2.6.20, created on 2011-10-25 17:26:06
         compiled from _core/pagemanager/mvc/control/handlepagestatpackage.tpl */ ?>
function HandlePageStatPackage(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options

  this._createElements();
};

HandlePageStatPackage.prototype._createElements = function() {
  var _self = this;
  this._popupBox = new PopupBox();
  this._search = DlmanageUtils.search();
  
  var pos = [window.screen.width / 2, window.screen.height / 2];
  MiscUtils.load(this._popupBox, pos, 'Loading...');
  
  this._loadData();
};

HandlePageStatPackage.prototype._loadData = function() {
  this._results = null;
  this._dateZones = null;
  this._retrieveResults(1);
  this._retrieveDateZones();
};

HandlePageStatPackage.prototype._verifyData = function() {
  if (this._results && this._dateZones) {
    this._updateElements();
  }
};

HandlePageStatPackage.prototype._retrieveResults = function(page, from, to) {
  var _self = this;
  var args = '&p=' + page + ((from) ?  '&from=' + from : '') + ((to) ?  '&to=' + to : '');
  new RequestUtils()._mysql('iestatspackage', args, function(result, params) { _self._results = result;
                                                                               _self._verifyData.call(_self);
                                                                             }, null);
};

HandlePageStatPackage.prototype._retrieveDateZones = function(page, condition, date, from, to, pos) {
  var _self = this;
   var arg ="&c=d.page=\'" + this._options.template + "\' ";
  /*new RequestUtils()._read('datezone', null, 'd.page=\'' + this._options.template + '\'', null, null, null, null, function(result, params) { _self._dateZones = result.data;
                                                                                                                                            _self._verifyData.call(_self);
                                                                                                                                          }, null);*/
   new RequestUtils()._mysql('datezone', arg, function(result, params) { _self._dateZones = result.data;
                                                                          _self._parameters = result;
                                                                          _self._verifyData.call(_self);
                                                                        }, {php:'customphp/mysqlread.php',pos: (pos) ? pos : null });                                                                                                                                             
};

HandlePageStatPackage.prototype._updateElements = function() {
  var _self = this;
  this._popupBox._close();
  DlmanageUtils.clear(this._gui.dateZone);
  DOMUtils.removeChildElements(this._gui.wedding.pTotal);
  DOMUtils.removeChildElements(this._gui.wedding.vTotal);
  DOMUtils.removeTableRows(this._gui.wedding.pTable, 1);
  DOMUtils.removeTableRows(this._gui.wedding.vTable, 1);
  
  DOMUtils.removeChildElements(this._gui.prewedding.pTotal);
  DOMUtils.removeChildElements(this._gui.prewedding.vTotal);
  DOMUtils.removeTableRows(this._gui.prewedding.pTable, 1);
  DOMUtils.removeTableRows(this._gui.prewedding.vTable, 1);
  
  DOMUtils.removeChildElements(this._gui.portrait.pTotal);
  DOMUtils.removeChildElements(this._gui.portrait.vTotal);
  DOMUtils.removeTableRows(this._gui.portrait.pTable, 1);
  DOMUtils.removeTableRows(this._gui.portrait.vTable, 1);
  
  DOMUtils.removeChildElements(this._gui.engagement.pTotal);
  DOMUtils.removeChildElements(this._gui.engagement.vTotal);
  DOMUtils.removeTableRows(this._gui.engagement.pTable, 1);
  DOMUtils.removeTableRows(this._gui.engagement.vTable, 1);
  
  DOMUtils.removeChildElements(this._gui.other.pTotal);
  DOMUtils.removeChildElements(this._gui.other.vTotal);
  DOMUtils.removeTableRows(this._gui.other.pTable, 1);
  DOMUtils.removeTableRows(this._gui.other.vTable, 1);

  var wedpt = 0, wedvt = 0, prept = 0, prevt = 0, porpt = 0, porvt = 0, engpt = 0, engvt = 0, othpt = 0, othvt = 0;
  /*--wedding--*/
  if (this._results.wedp.length == 0) {
    tr = this._gui.wedding.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.wedp.length; i < il; i++) {
    var wedp = this._results.wedp[i];
    wedpt += wedp.value;
    tr = this._gui.wedding.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(wedp.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(wedp.value));
  }
  this._gui.wedding.pTotal.appendChild(document.createTextNode(wedpt));
  if (this._results.wedv.length == 0) {
    tr = this._gui.wedding.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.wedv.length; i < il; i++) {
    var wedv = this._results.wedv[i];
    wedvt += wedv.value;
    tr = this._gui.wedding.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(wedv.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(wedv.value));
  }
  this._gui.wedding.vTotal.appendChild(document.createTextNode(wedvt));
  
  /*--prewedding--*/
  if (this._results.prep.length == 0) {
    tr = this._gui.prewedding.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.prep.length; i < il; i++) {
    var prep = this._results.prep[i];
    prept += prep.value;
    tr = this._gui.prewedding.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(prep.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(prep.value));
  }
  this._gui.prewedding.pTotal.appendChild(document.createTextNode(prept));
  if (this._results.prev.length == 0) {
    tr = this._gui.prewedding.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.prev.length; i < il; i++) {
    var prev = this._results.prev[i];
    prevt += prev.value;
    tr = this._gui.prewedding.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(prev.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(prev.value));
  }
  this._gui.prewedding.vTotal.appendChild(document.createTextNode(prevt));
  
  /*--portrait--*/
  if (this._results.porp.length == 0) {
    tr = this._gui.portrait.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.porp.length; i < il; i++) {
    var porp = this._results.porp[i];
    porpt += porp.value;
    tr = this._gui.portrait.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(porp.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(porp.value));
  }
  this._gui.portrait.pTotal.appendChild(document.createTextNode(porpt));
  if (this._results.porv.length == 0) {
    tr = this._gui.portrait.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.porv.length; i < il; i++) {
    var porv = this._results.porv[i];
    porvt += porv.value;
    tr = this._gui.portrait.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(porv.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(porv.value));
  }
  this._gui.portrait.vTotal.appendChild(document.createTextNode(porvt));
  
  /*--engagement--*/
  if (this._results.engp.length == 0) {
    tr = this._gui.engagement.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.engp.length; i < il; i++) {
    var engp = this._results.engp[i];
    engpt += engp.value;
    tr = this._gui.engagement.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(engp.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(engp.value));
  }
  this._gui.engagement.pTotal.appendChild(document.createTextNode(engpt));
  if (this._results.engv.length == 0) {
    tr = this._gui.engagement.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.engv.length; i < il; i++) {
    var engv = this._results.engv[i];
    engvt += engv.value;
    tr = this._gui.engagement.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(engv.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(engv.value));
  }
  this._gui.engagement.vTotal.appendChild(document.createTextNode(engvt));
  
  /*--other--*/
  if (this._results.othp.length == 0) {
    tr = this._gui.other.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.othp.length; i < il; i++) {
    var othp = this._results.othp[i];
    othpt += othp.value;
    tr = this._gui.other.pTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(othp.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(othp.value));
  }
  this._gui.other.pTotal.appendChild(document.createTextNode(othpt));
  if (this._results.othv.length == 0) {
    tr = this._gui.other.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.colSpan = 2;
    td.style.textAlign = 'center';
    td.style.height = '24px';
    td.appendChild(document.createTextNode('N/A'));
  }
  for (var i = 0, il = this._results.othv.length; i < il; i++) {
    var othv = this._results.othv[i];
    othvt += othv.value;
    tr = this._gui.other.vTable.insertRow(-1);
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td14';
    td.appendChild(document.createTextNode(othv.name));
    td = tr.insertCell(-1);
    td.className = 'elemstatpackagewedding_td13';
    td.appendChild(document.createTextNode(othv.value));
  }
  this._gui.other.vTotal.appendChild(document.createTextNode(othvt));
  
  
  /*--DATE ZONE----*/
  this._dateZone = { created: '',
                     to: ''
                   };
  this._gui.dateZone.dateCreated.value = (this._dateZone.created) ? SimpleDate.format(this._dateZone.created) : '';
  this._gui.dateZone.dateCreated.onclick = function() { var context = this;
                                                        _self._changeDate.call(_self, this, _self._dateZone.created, function(sd) { _self._dateZone.created = sd; context.focus(); }, false);
                                                        return false;
                                                      };
  this._gui.dateZone.dateTo.value = (this._dateZone.to) ? SimpleDate.format(this._dateZone.to) : '';
  this._gui.dateZone.dateTo.onclick = function() { var context = this;
                                                   _self._changeDate.call(_self, this, _self._dateZone.to, function(sd) { _self._dateZone.to = sd; context.focus(); }, false);
                                                   return false;
                                                 };
  this._gui.dateZone.create.onclick = function() { if (_self._dateZone.created &&_self._dateZone.to) {
                                                     var date = DateZone.instance();
                                                     date.start = _self._dateZone.created;
                                                     date.end = _self._dateZone.to;
                                                     date.page = _self._options.template;
                                                     new RequestUtils()._mwrite('datezone', [date], [], function(date, params) { _self._retrieveDateZones.call(_self);}, DOMUtils.findPos(this));
                                                   } else {
                                                     window.alert('DATE NOT BE EMPTY');
                                                   }
                                                 };
  var table = document.createElement('table');
  table.style.width = '166px';
  table.cellPadding = 0;
  table.cellSpacing = 0;
  this._gui.dateZone.result.appendChild(table);
  for (var i = 0, il = this._dateZones.length; i < il; i++) {
    var date = this._dateZones[i];
    tr = table.insertRow(-1);
    tr.className = (tr.rowIndex % 2 == 0) ? 'rowodd' : 'roweven';
    td = tr.insertCell(-1);
    td.style.width = '146px';
    td.style.height = '20px';
    td.style.textAlign = 'center';
    var a = document.createElement('a');
    a.href = '#';
    a._date = date;
    a.appendChild(document.createTextNode(SimpleDate.format(date.start) + ' - ' + SimpleDate.format(date.end)));
    a.onclick = function() { _self._search.dateFrom = this._date.start;
                             _self._search.dateTo = this._date.end;
                             _self._retrieveResults.call(_self, 1, JSON.stringify(this._date.start), JSON.stringify(this._date.end));
                             return false;
                           };
    td.appendChild(a);
    td = tr.insertCell(-1);
    td.style.width = '20px';
    td.style.textAlign = 'center';
    var span = document.createElement('span');
    span.appendChild(document.createTextNode('(x)'));
    span.style.cursor = 'pointer';
    span._date = date;
    span.onclick = function() { new RequestUtils()._mwrite('datezone', [], [this._date], function(date, params) { _self._retrieveDateZones.call(_self);}, null);
                              };
    td.appendChild(span);
  }
  
  this._customerSearch(this._gui, function(from, to) { _self._retrieveResults.call(_self, 1, from, to); 
                                                                });
};

HandlePageStatPackage.prototype._customerSearch = function(gui, callbackFunc) {
  var _self = this;
  this._callbackFunc = callbackFunc;

  gui.search.dateCreated.value = (this._search.dateFrom) ? SimpleDate.format(this._search.dateFrom) : '';
  gui.search.dateCreated.onclick = function() { var context = this;
                                                _self._changeDate.call(_self, this, _self._search.dateFrom, function(sd) { _self._search.dateFrom = sd; context.focus(); }, false);
                                                return false;
                                              };
  gui.search.dateCreated.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                                    if (code == 13) {
                                                      _self._callbackFunc.call(_self, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null);
                                                    }
                                                  };
  gui.search.dateTo.value = (this._search.dateTo) ? SimpleDate.format(this._search.dateTo) : '';
  gui.search.dateTo.onclick = function() { var context = this;
                                           _self._changeDate.call(_self, this, _self._search.dateTo, function(sd) { _self._search.dateTo = sd; context.focus(); }, false);
                                           return false;
                                         };
  gui.search.dateTo.onkeypress = function(e) { var code = DOMUtils.getEventCode(e);
                                               if (code == 13) {
                                                 _self._callbackFunc.call(_self, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null);
                                               }
                                             };
 gui.search.search.onclick = function() { _self._callbackFunc.call(_self, (_self._search.dateFrom) ? JSON.stringify(_self._search.dateFrom) : null, (_self._search.dateTo) ? JSON.stringify(_self._search.dateTo) : null); };
};

HandlePageStatPackage.prototype._changeDate = function(label, currentDate, onChangeFunc, showTime) {
  var _self = this;
  this._wContainer = document.createElement('div');
  this._wContainer.style.left = DOMUtils.findPos(label)[0] + 'px';
  this._wContainer.style.top = DOMUtils.findPos(label)[1] + 'px';
  this._popupBox._open(this._wContainer, { pos: DOMUtils.findPos(label) });
  
  var ds = new DateSelect(currentDate, null, { container: this._wContainer, showTime: showTime });
  ds._selectFunc = function(sd) { _self._selectDate.call(_self, sd, label, onChangeFunc, showTime); };
};

HandlePageStatPackage.prototype._selectDate = function(sd, label, onChangeFunc, showTime) {
  
  onChangeFunc(sd);
  this._showDate(sd, label, showTime);
  this._popupBox._close();
};

HandlePageStatPackage.prototype._showDate = function(sd, label, showTime) {
  if (sd) { 
    label.value = SimpleDate.format(sd, showTime);
  } else {
    label.value = '';
  }
};